// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:hawk/hawk.dart';
import 'package:nim_core/nim_core.dart';
import 'dart:io';

import 'package:path_provider/path_provider.dart';

import '../main_test.dart';

class HandleChatroomCase extends HandleBaseCase {
  HandleChatroomCase();

  final _robotMsgTypeMap = {
    "welcome": NIMRobotMessageType.welcome,
    "text": NIMRobotMessageType.text,
    "link": NIMRobotMessageType.link
  };

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);
    var handled = false;
    var isCallback = false;
    var result;
    var name = null;
    if (className?.trim() != 'ChatroomService' &&
        className?.trim() != 'ChatroomMessageBuilder') {
      return null;
    }
    var map;
    if (params!.isNotEmpty) {
      map = (params![0] as Map).cast<String, dynamic>();
    }
    switch (methodName) {
      case 'createChatroomTextMessage':
        {
          final roomId = map['roomId'] as String;
          final text = map['text'] as String;
          final ret = await ChatroomMessageBuilder.createChatroomTextMessage(
              roomId: roomId, text: text);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomImageMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final displayName = map['displayName'] as String?;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final ret = await ChatroomMessageBuilder.createChatroomImageMessage(
              roomId: roomId,
              filePath: filePath,
              displayName: displayName,
              nosScene: nosScene);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomAudioMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final duration = map['duration'] as int;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final ret = await ChatroomMessageBuilder.createChatroomAudioMessage(
              roomId: roomId,
              filePath: filePath,
              duration: duration,
              nosScene: nosScene);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomLocationMessage':
        {
          final roomId = map['roomId'] as String;
          final latitude = map['latitude'] as double;
          final longitude = map['longitude'] as double;
          final address = map['address'] as String;
          final ret =
              await ChatroomMessageBuilder.createChatroomLocationMessage(
                  roomId: roomId,
                  latitude: latitude,
                  longitude: longitude,
                  address: address);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomVideoMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final width = map['width'] as int;
          final duration = map['duration'] as int;
          final height = map['height'] as int;
          final displayName = map['displayName'] as String?;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final ret = await ChatroomMessageBuilder.createChatroomVideoMessage(
              roomId: roomId,
              filePath: filePath,
              width: width,
              height: height,
              duration: duration,
              displayName: displayName,
              nosScene: nosScene);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomFileMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final displayName = map['displayName'] as String;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final ret = await ChatroomMessageBuilder.createChatroomFileMessage(
              roomId: roomId,
              filePath: filePath,
              displayName: displayName,
              nosScene: nosScene);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomTipMessage':
        {
          final roomId = map['roomId'] as String;
          final ret = await ChatroomMessageBuilder.createChatroomTipMessage(
              roomId: roomId);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomRobotMessage':
        {
          final roomId = map['roomId'] as String;
          final robotAccount = map['robotAccount'] as String;
          final type = map['type'] as String;
          final text = map['text'] as String?;
          final content = map['content'] as String?;
          final target = map['target'] as String?;
          final params = map['params'] as String?;
          final ret = await ChatroomMessageBuilder.createChatroomRobotMessage(
              roomId: roomId,
              robotAccount: robotAccount,
              type: _robotMsgTypeMap[type]!,
              text: text,
              content: content,
              target: target,
              params: params);
          result = ret;
          handled = true;
          break;
        }
      case 'createChatroomCustomMessage':
        {
          final roomId = map['roomId'] as String;
          final attachment = NIMMessageAttachment.fromJson(
              (map['attachment'] as Map).cast<String, dynamic>());
          final ret = await ChatroomMessageBuilder.createChatroomCustomMessage(
              roomId: roomId, attachment: attachment);
          result = ret;
          handled = true;
          break;
        }
      case 'enterChatroom':
        {
          final ret = await NimCore.instance.chatroomService
              .enterChatroom(NIMChatroomEnterRequest.fromMap(map));
          result = ret;
          handled = true;
          break;
        }
      case 'exitChatroom':
        {
          String roomId = map['roomId'];
          final ret =
              await NimCore.instance.chatroomService.exitChatroom(roomId);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomMessage':
        {
          final message = NIMChatroomMessage.fromMap(
              (map['message'] as Map).cast<String, dynamic>());
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomMessage(message, resend);
          result = ret;
          handled = true;
          break;
        }

      case 'sendChatroomTextMessage':
        {
          final roomId = map['roomId'] as String;
          final text = map['text'] as String;
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomTextMessage(
                  roomId: roomId, text: text, resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomImageMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final displayName = map['displayName'] as String?;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomImageMessage(
                  roomId: roomId,
                  filePath: filePath,
                  displayName: displayName,
                  nosScene: nosScene,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomAudioMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final duration = map['duration'] as int;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomAudioMessage(
                  roomId: roomId,
                  filePath: filePath,
                  duration: duration,
                  nosScene: nosScene,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomLocationMessage':
        {
          final roomId = map['roomId'] as String;
          final latitude = map['latitude'] as double;
          final longitude = map['longitude'] as double;
          final address = map['address'] as String;
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomLocationMessage(
                  roomId: roomId,
                  latitude: latitude,
                  longitude: longitude,
                  address: address,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomVideoMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final width = map['width'] as int;
          final duration = map['duration'] as int;
          final height = map['height'] as int;
          final displayName = map['displayName'] as String?;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomVideoMessage(
                  roomId: roomId,
                  filePath: filePath,
                  duration: duration,
                  width: width,
                  height: height,
                  displayName: displayName,
                  nosScene: nosScene,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomFileMessage':
        {
          var filePath = map['filePath'] as String;
          if (Platform.isIOS && filePath.isNotEmpty) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/${filePath}';
          }
          final roomId = map['roomId'] as String;
          final displayName = map['displayName'] as String;
          final nosScene = map['nosScene'] as String? ?? 'defaultIm';
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomFileMessage(
                  roomId: roomId,
                  filePath: filePath,
                  displayName: displayName,
                  nosScene: nosScene,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomTipMessage':
        {
          final roomId = map['roomId'] as String;
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomTipMessage(roomId: roomId, resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomRobotMessage':
        {
          final roomId = map['roomId'] as String;
          final robotAccount = map['robotAccount'] as String;
          final type = map['type'] as String;
          final text = map['text'] as String?;
          final content = map['content'] as String?;
          final target = map['target'] as String?;
          final params = map['params'] as String?;
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomRobotMessage(
                  roomId: roomId,
                  robotAccount: robotAccount,
                  type: _robotMsgTypeMap[type]!,
                  text: text,
                  content: content,
                  target: target,
                  params: params,
                  resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'sendChatroomCustomMessage':
        {
          final roomId = map['roomId'] as String;
          final attachment = NIMMessageAttachment.fromJson(
              (map['attachment'] as Map?)?.cast<String, dynamic>());
          final resend = map['resend'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .sendChatroomCustomMessage(
                  roomId: roomId, attachment: attachment, resend: resend);
          result = ret;
          handled = true;
          break;
        }
      case 'downloadAttachment':
        {
          final message = NIMChatroomMessage.fromMap(
              (map['message'] as Map).cast<String, dynamic>());
          final thumb = map['thumb'] as bool? ?? false;
          final ret = await NimCore.instance.chatroomService
              .downloadAttachment(message, thumb);
          result = ret;
          handled = true;
          break;
        }
      case 'fetchMessageHistory':
        {
          name = "messageList";
          final roomId = map['roomId'] as String;
          final startTime = map['startTime'] as int;
          final limit = map['limit'] as int;
          final direction = map['direction'] as int;
          final messageTypeList = (map['messageTypeList'] as List?)?.map((e) {
            return NIMMessageTypeConverter().fromValue(e);
          }).toList();
          final ret = await NimCore.instance.chatroomService
              .fetchMessageHistory(
                  roomId: roomId,
                  startTime: startTime,
                  limit: limit,
                  direction: direction == QueryDirection.QUERY_NEW.index
                      ? QueryDirection.QUERY_NEW
                      : QueryDirection.QUERY_OLD,
                  messageTypeList: messageTypeList);
          result = ret;
          handled = true;
          break;
        }
      case 'fetchChatroomInfo':
        {
          final roomId = map['roomId'] as String;
          final ret =
              await NimCore.instance.chatroomService.fetchChatroomInfo(roomId);
          result = ret;
          handled = true;
          break;
        }
      case 'updateChatroomInfo':
        {
          final roomId = map['roomId'] as String;
          final needNotify = map['needNotify'] as bool? ?? true;
          final request = NIMChatroomUpdateRequest.fromMap(
              (map['request'] as Map).cast<String, Object>());
          final notifyExtension =
              (map['notifyExtension'] as Map?)?.cast<String, Object>();
          final ret = await NimCore.instance.chatroomService.updateChatroomInfo(
              roomId: roomId,
              request: request,
              needNotify: needNotify,
              notifyExtension: notifyExtension);
          result = ret;
          handled = true;
          break;
        }
      case 'fetchChatroomMembers':
        {
          name = "memberList";
          final chatroomMemberQueryTypeList = [
            NIMChatroomMemberQueryType.allNormalMember,
            NIMChatroomMemberQueryType.onlineNormalMember,
            NIMChatroomMemberQueryType.onlineGuestMemberByEnterTimeDesc,
            NIMChatroomMemberQueryType.onlineGuestMemberByEnterTimeAsc
          ];
          final roomId = map['roomId'] as String;
          final queryType = map['queryType'] as int;
          final limit = map['limit'] as int;
          final lastMemberAccount = map['lastMemberAccount'] as String?;
          final ret = await NimCore.instance.chatroomService
              .fetchChatroomMembers(
                  roomId: roomId,
                  queryType: chatroomMemberQueryTypeList[queryType],
                  limit: limit,
                  lastMemberAccount: lastMemberAccount);
          result = ret;
          handled = true;
          break;
        }
      case 'fetchChatroomMembersByAccount':
        {
          name = "memberList";
          final roomId = map['roomId'] as String;
          final accountList =
              (map['accountList'] as List).map((e) => e as String).toList();
          final ret = await NimCore.instance.chatroomService
              .fetchChatroomMembersByAccount(
                  roomId: roomId, accountList: accountList);
          result = ret;
          handled = true;
          break;
        }
      case 'updateChatroomMyMemberInfo':
        {
          final roomId = map['roomId'] as String;
          final request = NIMChatroomUpdateMyMemberInfoRequest.fromMap(
              map['request'].cast<String, dynamic>());
          final needNotify = map['needNotify'] as bool? ?? true;
          final notifyExtension =
              (map['notifyExtension'] as Map?)?.cast<String, dynamic>();
          final ret = await NimCore.instance.chatroomService
              .updateChatroomMyMemberInfo(
                  roomId: roomId,
                  request: request,
                  needNotify: needNotify,
                  notifyExtension: notifyExtension);
          result = ret;
          handled = true;
          break;
        }
      case 'markChatroomMemberBeManager':
        {
          final isAdd = map['isAdd'] as bool;
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .markChatroomMemberBeManager(isAdd: isAdd, options: options);
          result = ret;
          handled = true;
          break;
        }
      case 'markChatroomMemberBeNormal':
        {
          final isAdd = map['isAdd'] as bool;
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .markChatroomMemberBeNormal(isAdd: isAdd, options: options);
          result = ret;
          handled = true;
          break;
        }
      case 'kickChatroomMember':
        {
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .kickChatroomMember(options);
          result = ret;
          handled = true;
          break;
        }
      case 'markChatroomMemberInBlackList':
        {
          final isAdd = map['isAdd'] as bool;
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .markChatroomMemberInBlackList(isAdd: isAdd, options: options);
          result = ret;
          handled = true;
          break;
        }
      case 'markChatroomMemberMuted':
        {
          final isAdd = map['isAdd'] as bool;
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .markChatroomMemberMuted(isAdd: isAdd, options: options);
          result = ret;
          handled = true;
          break;
        }
      case 'markChatroomMemberTempMuted':
        {
          final duration = map['duration'] as int;
          final needNotify = map['needNotify'] as bool? ?? false;
          final options = NIMChatroomMemberOptions.fromMap(
              (map['options'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .markChatroomMemberTempMuted(
                  duration: duration, options: options, needNotify: needNotify);
          result = ret;
          handled = true;
          break;
        }
      case 'fetchChatroomQueue':
        {
          name = "entryList";
          final roomId = map['roomId'] as String;
          final ret =
              await NimCore.instance.chatroomService.fetchChatroomQueue(roomId);
          result = ret;
          handled = true;
          break;
        }
      case 'updateChatroomQueueEntry':
        {
          final roomId = map['roomId'] as String;
          final isTransient = map['isTransient'] as bool? ?? false;
          final entry = NIMChatroomQueueEntry.fromMap(
              (map['entry'] as Map).cast<String, dynamic>());
          final ret = await NimCore.instance.chatroomService
              .updateChatroomQueueEntry(
                  roomId: roomId, entry: entry, isTransient: isTransient);
          result = ret;
          handled = true;
          break;
        }
      case 'batchUpdateChatroomQueue':
        {
          name = "missingKeys";
          final roomId = map['roomId'] as String;
          final entryList = (map['entryList'] as List)
              .map((e) => NIMChatroomQueueEntry.fromMap(
                  (e as Map).cast<String, dynamic>()))
              .toList();
          final needNotify = map['needNotify'] as bool? ?? false;
          final notifyExtension =
              (map['notifyExtension'] as Map?)?.cast<String, dynamic>();
          final ret = await NimCore.instance.chatroomService
              .batchUpdateChatroomQueue(
                  roomId: roomId,
                  entryList: entryList,
                  needNotify: needNotify,
                  notifyExtension: notifyExtension);
          result = ret;
          handled = true;
          break;
        }
      case 'pollChatroomQueueEntry':
        {
          final roomId = map['roomId'] as String;
          final key = map['key'] as String?;
          final ret = await NimCore.instance.chatroomService
              .pollChatroomQueueEntry(roomId, key);
          result = ret;
          handled = true;
          break;
        }
      case 'clearChatroomQueue':
        {
          final roomId = map['roomId'] as String;
          final ret =
              await NimCore.instance.chatroomService.clearChatroomQueue(roomId);
          result = ret;
          handled = true;
          break;
        }
      case 'onEventNotified':
        {
          isCallback = true;
          subsriptions.add(
              NimCore.instance.chatroomService.onEventNotified.listen((event) {
            print('ChatroomService#onEventNotified ${event.toMap()}');
            IntegratedManager.instance.report(
                'onEventNotified', ResultBean(code: 0, data: event.toMap()));
          }));
          break;
        }
      case 'onMessageReceived':
        {
          isCallback = true;
          subsriptions.add(NimCore.instance.chatroomService.onMessageReceived
              .listen((event) {
            print(
                'ChatroomService#onMessageReceived ${event.map((e) => e.toMap()).toList()}');
            IntegratedManager.instance.report(
                'onMessageReceived',
                ResultBean(
                    code: 0, data: event.map((e) => e.toMap()).toList()));
          }));
          break;
        }
      case 'onMessageStatusChanged':
        {
          isCallback = true;
          subsriptions.add(NimCore
              .instance.chatroomService.onMessageStatusChanged
              .listen((event) {
            print('ChatroomService#onMessageStatusChanged ${event.toMap()}');
            IntegratedManager.instance.report('onMessageStatusChanged',
                ResultBean(code: 0, data: event.toMap()));
          }));
          break;
        }
      case 'onMessageAttachmentProgressUpdate':
        {
          isCallback = true;
          subsriptions.add(NimCore
              .instance.chatroomService.onMessageAttachmentProgressUpdate
              .listen((event) {
            print(
                'ChatroomService#onMessageAttachmentProgressUpdate ${event.toMap()}');
            IntegratedManager.instance.report(
                'onMessageAttachmentProgressUpdate',
                ResultBean(code: 0, data: event.toMap()));
          }));
          break;
        }
      case 'setIndependentModeLinkAddressProvider':
        {
          final linkAddress = (map['linkAddress'] as List<dynamic>)
              .map((e) => e.toString())
              .toList();
          NimCore.instance.chatroomService.independentModeLinkAddressProvider =
              (roomId, account) async {
            return linkAddress;
          };
          handleCaseResult = ResultBean.success(data: null);
          break;
        }
      // case 'dynamicChatroomTokenProvider':
      //   {
      //     final token = map['token'] as String;
      //     NimCore.instance.chatroomService.dynamicChatroomTokenProvider =
      //         (roomId, account) async {
      //       return token;
      //     };
      //     handleCaseResult = ResultBean.success(data: null);
      //     break;
      //   }
    }
    if (handled) {
      print('ChatroomService#$methodName result: ${result.code}');
      handleCaseResult = ResultBean(
        code: result.code,
        data:
            chatroomToMap(result.code, result.errorDetails, result.data, name),
        message: result.errorDetails,
      );
    }
    if (isCallback) {
      handleCaseResult = ResultBean.success(data: null);
    }
    return handleCaseResult;
  }

  Map<String, dynamic> memberToMap(NIMChatroomMember e) => <String, dynamic>{
        'roomId': e.roomId,
        'account': e.account,
        'memberType': _$NIMChatroomMemberTypeEnumMap[e.memberType]!,
        'nickname': e.nickname,
        'avatar': e.avatar,
        'extension': e.extension,
        'isOnline': e.isOnline,
        'isInBlackList': e.isInBlackList,
        'isMuted': e.isMuted,
        'isTempMuted': e.isTempMuted,
        'tempMuteDuration': e.tempMuteDuration,
        'isValid': e.isValid,
        'enterTime': e.enterTime,
        'tags': e.tags,
        'notifyTargetTags': e.notifyTargetTags,
      };

  Map<String, dynamic> resultToMap(NIMChatroomEnterResult e) =>
      <String, dynamic>{
        'roomId': e.roomId,
        'roomInfo': e.roomInfo == null ? null : infoToMap(e.roomInfo!),
        'member': e.member == null ? null : memberToMap(e.member!),
      };

  Map<String, dynamic> infoToMap(NIMChatroomInfo e) => <String, dynamic>{
        'roomId': e.roomId,
        'name': e.name,
        'announcement': e.announcement,
        'broadcastUrl': e.broadcastUrl,
        'creator': e.creator,
        'validFlag': e.validFlag,
        'onlineUserCount': e.onlineUserCount,
        'mute': e.mute,
        'extension': e.extension,
        'queueModificationLevel': _$NIMChatroomQueueModificationLevelEnumMap[
            e.queueModificationLevel]!,
      };

  Map<String, dynamic> chatroomToMap(
      int code, String? errorDetails, Object? data, String? name) {
    var dataMap = null;
    if (data is NIMChatroomMessage) {
      dataMap = data?.toMap();
    } else if (data is List<NIMChatroomMessage>) {
      // messageList
      var temp = data?.map((e) => e.toMap()).toList();
      dataMap = <String, dynamic>{
        name!: temp,
      };
    } else if (data is NIMChatroomMember) {
      dataMap = data == null ? null : memberToMap(data);
    } else if (data is NIMChatroomEnterResult) {
      dataMap = data == null ? null : resultToMap(data);
    } else if (data is NIMChatroomInfo) {
      dataMap = data == null ? null : infoToMap(data);
    } else if (data is NIMChatroomQueueEntry) {
      dataMap = data?.toMap();
    } else if (data is List<NIMChatroomMember>) {
      var temp = data?.map((e) => memberToMap(e)).toList();
      if (name == null) {
        dataMap = temp;
      } else {
        dataMap = <String, dynamic>{
          name!: temp,
        };
      }
    } else if (data is List<NIMChatroomQueueEntry>) {
      // entryList
      var temp = data?.map((e) => e.toMap()).toList();
      dataMap = <String, dynamic>{
        name!: temp,
      };
    } else if (name != null && dataMap == null) {
      dataMap = <String, dynamic>{name: data};
    }
    return {
      'code': code,
      'data': dataMap ?? data,
      'msg': errorDetails,
    };
  }
}

const _$NIMChatroomQueueModificationLevelEnumMap = {
  NIMChatroomQueueModificationLevel.anyone: 'anyone',
  NIMChatroomQueueModificationLevel.manager: 'manager',
};

const _$NIMChatroomMemberTypeEnumMap = {
  NIMChatroomMemberType.unknown: 'unknown',
  NIMChatroomMemberType.guest: 'guest',
  NIMChatroomMemberType.restricted: 'restricted',
  NIMChatroomMemberType.normal: 'normal',
  NIMChatroomMemberType.creator: 'creator',
  NIMChatroomMemberType.manager: 'manager',
  NIMChatroomMemberType.anonymous: 'anonymous',
};
